<template>
  <div>
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row type="flex" align="middle">
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="充值类型">
                <el-select v-model="query.keywords" placeholder="请选择充值类型">
                  <el-option label="拉新奖励" value="3"></el-option>
                  <el-option label="前台充值" value="2"></el-option>
                  <el-option label="后台充值" value="1"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item style="margin-bottom:0px" label="总额">
                <div>{{sumMoney}}</div>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="box-card">
      <el-table
        style="margin-bottom:20px"
        :header-cell-style="{background:'#eee'}"
        border
        :data="tableData"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="money" label="充值金额"></el-table-column>
        <el-table-column align="center" prop="money" label="充值方式">
          <template slot-scope="scoped">
            <div v-if="scoped.row.pay_type==1">微信</div>
            <div v-else-if="scoped.row.pay_type==2">支付宝</div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="add_time" label="时间"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { userRecharge } from '@/api/consumer'
import { parseTime } from '@/utils/index'
export default {
  props: {
    user_id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //input
      query: {
        keywords: '2'
      },
      tableData: [],
      sumMoney: ''
    }
  },
  mounted() {
    // this.getRechargeList();
  },
  watch: {
    user_id(newVal, oldVal) {
      this.getRechargeList()
    }
  },
  methods: {
    // 获取优惠券列表
    async getRechargeList() {
      const res = await userRecharge({
        user_id: this.user_id,
        page: this.currentPage,
        limit: this.pageSize,
        type: this.query.keywords
      })
      console.log(res)
      this.tableData = res.data
      this.sumMoney = res.sum_money
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.use_time == 0) {
          item.use_time = '-'
        } else {
          item.use_time = parseTime(item.use_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.pass_time == 0) {
          item.pass_time = '-'
        } else {
          item.pass_time = parseTime(item.pass_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
      })
      this.total = res.count
    },
    queryAsk() {
      this.currentPage = 1
      this.getRechargeList()
    },
    //重置
    queryReset() {
      this.resetForm('query')
      this.total = 0
      this.currentPage = 1
      this.pageSize = 10
      this.query.keywords = ''
      this.getRechargeList()
    },

    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getRechargeList()
    }
  }
}
</script>

<style lang='scss' scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input {
  width: 292px;
  display: block;
  position: static !important;
}
::v-deep .el-input--medium .el-input__inner {
  width: 292px;
}

::v-deep .el-form-item__label {
  padding: 0 30px 0 0;
}
::v-deep .el-form {
  display: flex;
}
</style>